package com.lz.sports.utils

import android.graphics.*
import java.io.*


//默认压缩尺寸限制大小
private const val DEF_WIDTH = 1280
private const val DEF_HEIGHT = 960

fun compressBitmapAndSave(path: String, w: Int = DEF_WIDTH, h: Int = DEF_HEIGHT): Bitmap? {
    val bitmap = compressBitmap(path, w, h)?.also {
        saveBitmapFile(it, path, false)
    }
    return bitmap
}

//宽高压缩
fun compressBitmap(path: String, w: Int, h: Int): Bitmap? {
    try {
        val opts = BitmapFactory.Options().apply { inJustDecodeBounds = true }
        BitmapFactory.decodeFile(path, opts)
        val scaleX: Int = opts.outWidth / w
        val scaleY: Int = opts.outHeight / h
        var scale = 1
        if (scaleX >= scaleY && scaleX >= 1) {
            scale = scaleX
        }
        if (scaleX < scaleY && scaleY >= 1) {
            scale = scaleY
        }
        opts.inJustDecodeBounds = false
        opts.inSampleSize = scale
        return BitmapFactory.decodeFile(path, opts)
    } catch (_: Exception) {
    }
    return null
}

/**
 * 旋转Bitmap
 */
fun rotateBitmap(bitmap: Bitmap, degree: Float, recycler: Boolean = true): Bitmap {
    if (degree == 0f) return bitmap
    val matrix = Matrix()
    matrix.setRotate(degree)
    val newBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.width, bitmap.height, matrix, true)
    if (recycler) {
        recycleBitmap(bitmap)
    }
    return newBitmap
}

/**
 * 保存Bitmap到文件中
 *
 * @param bitmap
 */
fun saveBitmapFile(bitmap: Bitmap, path: String, isRecycler: Boolean): Boolean {
    try {
        val bos = BufferedOutputStream(FileOutputStream(path))
        bitmap.compress(Bitmap.CompressFormat.JPEG, 80, bos)
        bos.flush()
        bos.close()
        if (isRecycler && !bitmap.isRecycled) {
            bitmap.recycle()
        }
        return true
    } catch (e: java.lang.Exception) {
        e.printStackTrace()
    }
    return false
}

/**
 * 保存Bitmap到文件中
 *
 * @param bitmap
 */
fun saveBitmapFile(bitmap: Bitmap, file: File, isRecycler: Boolean): Boolean {
    try {
        val bos = BufferedOutputStream(FileOutputStream(file))
        bitmap.compress(Bitmap.CompressFormat.JPEG, 80, bos)
        bos.flush()
        bos.close()
        if (isRecycler && !bitmap.isRecycled) {
            bitmap.recycle()
        }
        return true
    } catch (e: java.lang.Exception) {
        e.printStackTrace()
    }
    return false
}

/**
 * 回收Bitmap
 */
private fun recycleBitmap(bitmap: Bitmap?) {
    if (bitmap != null && !bitmap.isRecycled) {
        bitmap.recycle()
    }
}

//NV21转bitmap
fun nv21ToBitmap(nv21: ByteArray, width: Int, height: Int): Bitmap? {
    var bitmap: Bitmap? = null
    try {
        val image = YuvImage(nv21, ImageFormat.NV21, width, height, null)
        val stream = ByteArrayOutputStream()
        image.compressToJpeg(Rect(0, 0, width, height), 100, stream)
        bitmap = BitmapFactory.decodeByteArray(stream.toByteArray(), 0, stream.size())
        stream.close()
    } catch (e: IOException) {
    }
    return bitmap
}

fun saveNv21(nv21: ByteArray, width: Int, height: Int, path: String, isRecycler: Boolean = true): Boolean {
    val bitmap = nv21ToBitmap(nv21, width, height)
    if (bitmap != null) {
        return saveBitmapFile(bitmap, path, isRecycler)
    }
    return false
}

fun saveNv21(nv21: ByteArray, width: Int, height: Int, f: File, isRecycler: Boolean = true): Boolean {
    val bitmap = nv21ToBitmap(nv21, width, height)
    if (bitmap != null) {
        return saveBitmapFile(bitmap, f, isRecycler)
    }
    return false
}